package com.example.compose

import android.os.Bundle
import android.util.Log
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.activity.viewModels
import androidx.compose.foundation.ExperimentalFoundationApi
import androidx.compose.foundation.Image
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.lazy.GridCells
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.LazyVerticalGrid
import androidx.compose.foundation.lazy.items
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.MaterialTheme
import androidx.compose.material.Surface
import androidx.compose.material.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.navigation.NavController
import androidx.navigation.compose.NavHost
import androidx.navigation.compose.composable
import androidx.navigation.compose.navigate
import androidx.navigation.compose.rememberNavController
import com.example.compose.ui.theme.ComposeTheme
import com.example.compose.viewmodel.DetailViewModel
import com.google.accompanist.coil.rememberCoilPainter
import dagger.hilt.android.AndroidEntryPoint
import java.text.SimpleDateFormat
import java.util.*

@AndroidEntryPoint
class DetailActivity : ComponentActivity() {
    val viewModel: DetailViewModel by viewModels()

    //    private lateinit var detailImgRvAdapter: DetailImgRvAdapter
    private val dateFormat = SimpleDateFormat("yyyy-MM-dd hh:mm", Locale.CHINA)

    //    private var favouriteInfor: FavouriteInfor? = null
//    private var historyInfor: HistoryInfor? = null
    private var historyMark = 0//记录在list位置
    private var favouriteMark = 0//标记第几个喜爱

    //    private val favouriteViewModel: FavouriteViewModel by activityViewModels()
    private var fragmentMark: Int? = null

    //    private lateinit var favouriteDialogRvAdapter: DetailRvAdapter
    private var y2: Float = 0f
    private var tranY: Float = 0f
    private var y: Float = 0f
    private var isLike: Boolean = false

    //    private lateinit var historyList: MutableList<HistoryInfor>
    @ExperimentalFoundationApi
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        val extras = intent.extras
        Log.i("TAG", "DetailActivity_onCreate: ")
        setContent {
            val navController = rememberNavController()
            viewModel.msg4LiveData.observe(this) {
                navController.navigate("img")
            }
            NavHost(navController = navController, startDestination = "detail") {
                composable("detail") {
                    ComposeTheme {
                        Surface(color = MaterialTheme.colors.background) {
                            Greeting2(
                                extras?.getString("img") ?: "",
                                extras?.getString("name") ?: "",
                                viewModel,
                                this@DetailActivity
                            )
                        }
                    }
                }
                composable("img") {
                    Img(navController, viewModel)
                }
            }

        }

    }

    override fun onBackPressed() {
        super.onBackPressed()
        viewModel.onMsg4Dismiss()
    }

    /**更新观看集数并加载漫画**/
    private fun update(p: Int) {
        if (viewModel.pgLiveData.value == false) return
        Log.i(TAG, "his:$historyMark ")
//        viewModel.msg3Send(p)
//        favouriteDialogRvAdapter.itemChange(p)
//        historyInfor?.mark = p//当前页面
//        historyList[historyMark].mark = p//历史list
//        with(favouriteViewModel) {
//            historyUpdate(historyInfor!!)//历史数据库
//            historyLivaData.value = historyMark
//            if (isLike) {
//                favouriteInfor?.mark = p//当前页面
//                favouriteUpdate(favouriteInfor)//喜爱数据库
//            }
//        }
    }

    override fun onDestroy() {
        super.onDestroy()
        viewModel.onMsg3Dismiss()
    }
}

@ExperimentalFoundationApi
@Composable
fun Greeting2(
    img: String,
    title: String,
    viewModel: DetailViewModel,
    activity: DetailActivity
) {
    Log.i("TAG", "Greeting2: ")
    Column(
        horizontalAlignment = Alignment.CenterHorizontally, modifier = Modifier
            .padding(4.dp)
            .wrapContentHeight()
            .fillMaxWidth()
    ) {
        Row(modifier = Modifier.fillMaxWidth()) {
            Image(painterResource(id = R.drawable.ic_baseline_arrow_back_ios_24),
                contentDescription = "image",
                modifier = Modifier
                    .height(40.dp)
                    .width(40.dp)
                    .clickable {
                        activity.finish()
                    })
        }
        Image(
            painter = rememberCoilPainter(request = img),
            contentDescription = "image",
            contentScale = ContentScale.Crop,
            modifier = Modifier
                .height(150.dp)
        )
        Text(text = title, maxLines = 1)
        Text(text = viewModel.content ?: "")
        Text(text = "时间：${viewModel.update ?: ""}", fontSize = 10.sp, maxLines = 1)
        LazyVerticalGrid(cells = GridCells.Fixed(5)) {
            items(viewModel.msg3List) {
                Text(
                    text = it.title,
                    modifier = Modifier
                        .clip(shape = RoundedCornerShape(4.dp))
                        .clickable {
                            viewModel.msg3Send(it)
                        }
                )
            }
        }
    }
}

@Composable
fun Img(navController: NavController, viewModel: DetailViewModel) {
    Column {
        Image(painterResource(id = R.drawable.ic_baseline_arrow_back_ios_24),
            contentDescription = "image",
            modifier = Modifier
                .height(40.dp)
                .width(40.dp)
                .clickable {
                    viewModel.onMsg4Dismiss()
                    navController.navigateUp()
                })
        Log.i("TAG", "_Img:${viewModel.imgUrlList.size} ")
        LazyColumn() {
            items(viewModel.imgUrlList) { img ->
                Image(
                    painter = rememberCoilPainter(
                        request = img,
                    ),
                    contentDescription = "image",
                    modifier = Modifier
                        .fillParentMaxHeight()
                        .fillMaxWidth()
                )
            }
        }
    }
}